package com.qf.controller;

import com.qf.config.RedisLockUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
public class SecondKillController {

    //商品库存
    private static final Map<String, Long> itemStock = new HashMap<>();
    //商品订单
    private static final Map<String, Long> itemOrder = new HashMap<>();


    @Autowired
    CuratorFramework client;

    static {
        itemStock.put("computer", 1000L);
        itemOrder.put("computer", 0L);
    }

    @Autowired
    RedisLockUtil lockUtil;


    @RequestMapping("/kill")
    public String redisSecondKill(String item) throws InterruptedException {
        if (lockUtil.lock(item, "1", 10)) {

            //1. 获取 商品 信息
            //获取商品的剩余数量
            Long count = itemStock.get(item);

            //2. 判断商品是否已经卖完
            //2.1 如果没有卖完，继续卖
            if (count > 0) {

                Thread.sleep(100);
                //库存数量 - 1
                itemStock.put(item, count - 1);
                Thread.sleep(100);

                itemOrder.put(item, itemOrder.get(item) + 1);

                //释放锁
                lockUtil.unlock(item);

                return "商品：" + item + "剩余" + (itemStock.get(item)) + "; 已卖出:" + item + (itemOrder.get(item));
            } else {

                //释放锁
                lockUtil.unlock(item);
                //2.2 如果已经卖完，提示已卖完
                return "商品：" + item + "已卖完" + "; 已卖出:" + item + (itemOrder.get(item));
            }
        }

        return "获取锁失败";

    }

    @RequestMapping("/zkKill")
    public String zkSecondKill(String item) throws InterruptedException {
        //参数1： 客户端对象   参数2：监听的节点
        InterProcessMutex lock = new InterProcessMutex(client, "/abc");


        try {
            //获取锁， 最大等待时间为 2秒钟
            if(lock.acquire(2, TimeUnit.MINUTES)) {
                //1. 获取 商品 信息
                //获取商品的剩余数量
                Long count = itemStock.get(item);

                //2. 判断商品是否已经卖完
                //2.1 如果没有卖完，继续卖
                if(count > 0) {

                    Thread.sleep(100);
                    //库存数量 - 1
                    itemStock.put(item, count -1 );
                    Thread.sleep(100);

                    itemOrder.put(item, itemOrder.get(item) + 1);

                    return "商品："+item+"剩余"+ (itemStock.get(item)) + "; 已卖出:" + item + (itemOrder.get(item));
                }else {
                    //2.2 如果已经卖完，提示已卖完
                    return "商品："+item+"已卖完" + "; 已卖出:" + item + (itemOrder.get(item));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放锁资源
            try {
                lock.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return "系统异常";
    }
}
